//点击去个人中心
const userInfo = document.querySelector('.userInfo');
userInfo.addEventListener('.click', () => {
    location.href = "/user/personal_page";
})
const notificationText = document.querySelector('.notification-text');
notificationText.addEventListener('click', () => {
    location.href = "/user/recommendPage";
})
//侧边栏
const menuItemPost = document.querySelector('.menu-item-post');
const listFirst = document.querySelector('.list-first');
const menuItemAdmin = document.querySelector('.menu-item-admin');
const listSecond = document.querySelector('.list-second');
const menuItemPostTitle = document.querySelector('.menu-item-post-title');


listFirst.style.maxHeight = '150px';
//展开和收起创作列表
menuItemPost.addEventListener('click', function () {
    console.log("一点击");
    if (listFirst.style.maxHeight == '150px') {
        console.log("手气");
        // 收起创作子菜单
        listFirst.style.maxHeight = '0px';
        // 让管理菜单顺势往上移动
        menuItemAdmin.style.transform = 'translateY(0px)';
    }
    else {

        console.log("移动");
        // 展开创作子菜单
        listFirst.style.maxHeight = '150px';
        // 让管理菜单回到原来的位置
        menuItemAdmin.style.transform = 'translateY(px)';
    }
})
//展开和收起管理列表
listSecond.style.maxHeight = '100px';
menuItemPostTitle.addEventListener('click', function () {
    if (listSecond.style.maxHeight == '100px') {
        listSecond.style.maxHeight = '0';
    } else {
        listSecond.style.maxHeight = '100px';

    }
})
const alert = document.querySelector('.alert');
function showAlert(message, color) {
    alert.textContent = message;
    alert.style.color = color;
    alert.style.display = 'block';
    setTimeout(() => {
        alert.style.display = 'none';
    }, 3000);
}
// //四个分类
// const messagesButton = document.querySelector('.messages-button');
// const commentButton = document.querySelector('.comment-button');
// const fansButton = document.querySelector('.fans-button');
// const likeButton = document.querySelector('.like-button');
// const messageContent = document.querySelector('#message');
// const commentContent = document.querySelector('#comment');
// const fansContent = document.querySelector('#fans');
// const likesContent = document.querySelector('#likes');
//
// // 初始时隐藏除messageContent外的其他内容区域
// commentContent.classList.add('hidden');
// fansContent.classList.add('hidden');
// likesContent.classList.add('hidden');
//
// messagesButton.addEventListener('click', function () {
//     console.log("1");
//     // 显示messageContent
//     messageContent.classList.add('visible');
//     messageContent.classList.remove('hidden');
//     // 隐藏其他内容区域const computedStyle=window.getComputedStyle(messageContent);
//
//     commentContent.classList.add('hidden');
//     commentContent.classList.remove('visible');
//
//     const computedStyle = window.getComputedStyle(commentContent);
//
//     const display = computedStyle.getPropertyValue('display');
//     console.log(display);
//     fansContent.classList.add('hidden');
//     fansContent.classList.remove('visible');
//
//     likesContent.classList.add('hidden');
//     likesContent.classList.remove('visible');
//
// });
//
// commentButton.addEventListener('click', function () {
//     console.log("2");
//     // 显示commentContent
//     commentContent.classList.add('visible');
//     commentContent.classList.remove('hidden');
//     // 隐藏其他内容区域
//     messageContent.classList.add('hidden');
//     messageContent.classList.remove('visible');
//
//     const computedStyle = window.getComputedStyle(messageContent);
//     const display = computedStyle.getPropertyValue('display');
//     console.log(display);
//     fansContent.classList.add('hidden');
//     fansContent.classList.remove('visible');
//
//     likesContent.classList.add('hidden');
//     likesContent.classList.remove('visible');
//
// });
//
// fansButton.addEventListener('click', function () {
//     console.log("3");
//     // 显示fansContent
//     fansContent.classList.add('visible');
//     fansContent.classList.remove('hidden');
//     // 隐藏其他内容区域
//     messageContent.classList.add('hidden');
//     messageContent.classList.remove('visible');
//
//     commentContent.classList.add('hidden');
//     commentContent.classList.remove('visible');
//
//     likesContent.classList.add('hidden');
//     likesContent.classList.remove('visible');
//
// });
//
// likeButton.addEventListener('click', function () {
//     console.log("4");
//     // 显示likesContent
//     likesContent.classList.add('visible');
//     likesContent.classList.remove('hidden');
//     // 隐藏其他内容区域
//     messageContent.classList.add('hidden');
//     messageContent.classList.remove('visible');
//
//     commentContent.classList.add('hidden');
//     commentContent.classList.remove('visible');
//
//     fansContent.classList.add('hidden');
//     fansContent.classList.remove('visible');
//
// });

//三个管理的显示与隐藏
//获取三个标签和内容块
// const content = document.querySelector('.content');//整体的内容
// const secondLists = document.querySelector('.list-second');
// console.log(secondLists);
// const subMenuItems = secondLists.querySelectorAll('.sub-menu-item');
// console.log(subMenuItems);
// const items = content.querySelectorAll('.item');
//
// subMenuItems.forEach(menuItem => {
//     menuItem.addEventListener('click', () => {
//         console.log("已点击");
//         console.log(menuItem)
//         //移除所有标签的激活状态
//         subMenuItems.forEach(list => list.classList.remove('active'));
//         //给所点击的那个标签添加激活状态
//         menuItem.classList.add('active');
//         //获取当前所点击的标签的分类
//         const category = menuItem.dataset.category;
//         console.log(category);
//         //隐藏所有内容,显示对应分类
//         items.forEach(item => {
//             item.classList.remove('active');
//             if (item.dataset.category === category) {
//                 item.classList.add('active');
//             }
//         });
//     })
// })
//点击去创作
const postEssays = document.querySelectorAll('.postEssay');
postEssays.forEach(postEssay => {
    postEssay.addEventListener('click', () => {
        console.log("zhunbeiqvchuangzuo ");
        location.href = "/NewsController/toPost";
    })
})
function goToEssayPage(postId, userId) {
    //将当前文章的postId和作者id存储起来
    // localStorage.setItem('currentPostId', postId);
    // localStorage.setItem('authorId', userId);
    // console.log(typeof postId);//string
    const postIdNum = parseInt(postId);
    window.location.href = `/NewsController/${postIdNum}`;
}
const workItemLists = document.querySelector('.work-item-lists');
const tabItems = document.querySelectorAll('.tab-item');
const outLoading = document.querySelector('.outLoading');
//加载全部文章
const allButton = document.querySelector('.all');

allButton.addEventListener('click', () => {
    tabItems.forEach(item => item.classList.remove('active'));
    allButton.classList.add('active');
    loadAllPosts();
})
function loadAllPosts(){
    workItemLists.innerHTML=`<div class="outLoading"">
                    <div id="loading" class="loading">
                        <div class="spinner"></div>
                    </div>
                </div>`;
    // outLoading.style.display='block';
    // console.log(outLoading.style.display);
    setTimeout(()=>{
        $.ajax({
            url:"/selectUserPost",
            type:"GET",
            success:function(result){
                console.log(result);
                const allPostsList=result.data;
                // outLoading.style.display='none';
                // workItemLists.style.display='block';
                renderAllPosts(allPostsList);
                const currentPostCount=document.querySelector('.currentPostCount');
                if(allPostsList!=null){
                    currentPostCount.textContent=allPostsList.posts.length;
                }else{
                    currentPostCount.textContent='0';
                }
            },
            error:function(error){
                console.log(error);

            }

        })
    },500);

}
loadAllPosts();
function createNoContent(){
    return` <div class="tab-pane" id="noContent">
                                <div class="placeholder">
                                    <div class="img-text">
                                        <img class="empty" alt=""
                                             src="//lf-content-ecology.toutiaostatic.com/obj/tt-content-ecology-fe/pgcfe/mp/personal/9d8f0f4caae2008639742015a7dcd07b.png">
                                        <p class="placeholder-text">暂时没有对应的内容</p>
                                    </div>

                                </div>
                            </div>`;

}
function renderAllPosts(PostsList) {
    let html = "";
    if (PostsList === null) {
        html+=createNoContent()
    } else {
        const count =PostsList.posts.length;
        html += `
<div class="posts-num">共
                    <span class="currentPostCount">${count}</span>
                    条内容
                </div>`
        PostsList.posts.forEach(post => {
            html += `
                <div class="work-item-help-div" data-post-id="${post.postId}">
                    <div class="work-item"  data-post-id="${post.postId}"  data-user-id="${post.userId}">
                   
                        <div class="work-thumbnail">
                            <img src="${post.coverImage}" alt="封面">
                        </div>
                        <div class="work-info">
                            <div class="work-top">
                                <div class="work-title" data-post-id="${post.postId}" data-user-id="${post.userId}">
                                    ${post.title}
                                </div>
                                <div class="work-date">${post.createdTime}</div>
                            </div>
                            <div class="work-status">
                                    <span class="tag"></span>

                                </div>
                            <div class="work-actions">
                                    <ul class="work-stats">
                                        <li> · 收藏
                                            <span>${post.viewCount}</span>
                                        </li>
                                        <li>· 点赞
                                            <span>${post.likeCount}</span>
                                        </li>
                                        <li>· 评论
                                            <span class="currentPostCommentCount">${post.commentCount}</span>
                                        </li>
                                    </ul>


                                    <div class="actions-right">
                                        <button class="post-comment-button" data-post-id="${post.postId}">
                                           
                                            查看评论
                                        </button>
                                        <button class="modify" data-post-id="${post.postId}">
                                            修改
                                        </button>
                                        <button class="post-delete-button" data-post-id="${post.postId}">
                                           
                                            删除</button>
                                    </div>
                                   
                                </div>
                        </div>
                    </div>
                </div>
            `;
        });
    }

    workItemLists.innerHTML = html;
}
//加载草稿箱
const draftsButton = document.querySelector('.drafts');
draftsButton.addEventListener('click', () => {
    tabItems.forEach(item => item.classList.remove('active'));
    draftsButton.classList.add('active');
    loadDrafts(1);
})
function loadDrafts(pageNum) {
    workItemLists.innerHTML=`<div class="outLoading"">
                    <div id="loading" class="loading">
                        <div class="spinner"></div>
                    </div>
                </div>`;
    setTimeout(()=>{
        $.ajax({
            url: "/selectDraft",
            type: "GET",
            data: {
                pageNum: pageNum
            },
            success: function (result) {
                console.log("草稿箱数据加载成功", result);
                renderDrafts(result.data.posts);
                const currentPostCount=document.querySelector('.currentPostCount');
                if(result.data!=null){
                    currentPostCount.textContent=result.data.posts.length;
                }else{
                    currentPostCount.textContent='0';
                }

            },
            error: function (error) {
                console.error("加载草稿箱数据失败", error);
                workItemLists.innerHTML = "<div class='placeholder'>暂无草稿</div>";
            }
        });

    },500)

}
function renderDrafts(draftsList) {

    let html = "";

    if (draftsList.length === 0) {
        html = `
            <div class="placeholder">
                <div class="img-text">
                    <img class="empty" alt="暂无草稿" src="">
                    <p class="placeholder-text">暂无草稿</p>
                </div>
            </div>
        `;
    } else {
        const count =draftsList.length;
        html += `
  <div class="posts-num">共
                    <span class="currentPostCount">${count}</span>
                    条内容
                </div>`
        draftsList.forEach(draft => {
            html += `
                <div class="work-item-help-div" data-post-id="${draft.postId}">
                    <div class="work-item">
                        <span class="news-postId" style="display:none">${draft.postId}</span>
                        <span class="news-userId" style="display:none">${draft.userId}</span>
                        <div class="work-thumbnail">
                            <img src="${draft.coverImage}" alt="草稿封面">
                        </div>
                        <div class="work-info">
                            <div class="work-top">
                                <div class="work-title" data-post-id="${draft.postId}" data-user-id="${draft.userId}">
                                    ${draft.title}
                                </div>
                                <div class="work-date">${draft.createdTime}</div>
                            </div>
                            <div class="work-status">
                                <span class="tag draft-tag">草稿</span>
                            </div>
                            <div class="work-actions">
                            <div></div>
                                <div class="actions-right">
                                    <button class="modify-draft modify" data-post-id="${draft.postId}">
                                    <span class="postId-delete" style="display: none"
                                                th:text="${draft.postId}"></span>
                                        继续编辑
                                    </button>
                                    <button class="delete-draft post-delete-button" data-post-id="${draft.postId}">
                                        <span class="postId-delete" style="display: none"
                                                th:text="${draft.postId}"></span>
                                        删除草稿
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            `;
        });
    }

    workItemLists.innerHTML = html;
    // bindDraftActions(); // 绑定草稿操作事件（编辑/删除）
}
//查询已发布的文章
const postedButton = document.querySelector('.posted');
postedButton.addEventListener('click', () => {
    tabItems.forEach(item => item.classList.remove('active'));
    postedButton.classList.add('active');
    posted()
})
function posted(){
    workItemLists.innerHTML=`<div class="outLoading"">
                    <div id="loading" class="loading">
                        <div class="spinner"></div>
                    </div>
                </div>`;
    setTimeout(()=>{
        $.ajax({
            url:"/selectUserPost",
            type:"GET",
            data:{
                auditStatus:1
            },
            success:function(result){
                console.log(result);
                const PostedList=result.data;
                renderAllPosts(PostedList);
                const currentPostCount=document.querySelector('.currentPostCount');
                if(PostedList!=null){
                    currentPostCount.textContent=PostedList.posts.length;
                }else{
                    currentPostCount.textContent='0';
                }
            },
            error:function(error){
                console.log(error);

            }

        });

    },500)

}

//查询正在审核中的文章
const reviewingButton = document.querySelector('.reviewing');
reviewingButton.addEventListener('click', () => {
    tabItems.forEach(item => item.classList.remove('active'));
    reviewingButton.classList.add('active');
    isBeingReviewed()
})
function isBeingReviewed(){
    workItemLists.innerHTML=`<div class="outLoading"">
                    <div id="loading" class="loading">
                        <div class="spinner"></div>
                    </div>
                </div>`;
    setTimeout(()=>{
        $.ajax({
            url:"/selectUserPost",
            type:"GET",
            data:{
                auditStatus:0
            },
            success:function(result){
                console.log(result);
                const PostedList=result.data;
                renderAllPosts(PostedList);
                const currentPostCount=document.querySelector('.currentPostCount');
                if(PostedList!=null){
                    currentPostCount.textContent=PostedList.posts.length;
                }else{
                    currentPostCount.textContent='0';
                }
            },
            error:function(error){
                console.log(error);

            }

        })

    },500)

}
//查询审核未通过的文章
const rejectButton = document.querySelector('.reject');
rejectButton.addEventListener('click', () => {
    tabItems.forEach(item => item.classList.remove('active'));
    rejectButton.classList.add('active');
    rejectPost()
})
function rejectPost(){
    workItemLists.innerHTML=`<div class="outLoading"">
                    <div id="loading" class="loading">
                        <div class="spinner"></div>
                    </div>
                </div>`;
    setTimeout(()=>{
        $.ajax({
            url:"/selectUserPost",
            type:"GET",
            data:{
                auditStatus:2
            },
            success:function(result){
                console.log(result);
                const PostedList=result.data;
                renderAllPosts(PostedList);
                const currentPostCount=document.querySelector('.currentPostCount');
                if(PostedList!=null){
                    currentPostCount.textContent=PostedList.posts.length;
                }
            },
            error:function(error){
                console.log(error);

            }

        })
    },500)

}
//查看文章的全部评论的弹窗
// const postCommentButtons = document.querySelectorAll('.post-comment-button');
const commentPopup = document.querySelector('.comment-popup');
const closeButton = document.querySelector('.close-button');
    workItemLists.addEventListener('click', (event) => {
        const postCommentButton=event.target.closest('.post-comment-button');
        if(postCommentButton){
            event.stopPropagation();//阻止事件冒泡
            commentPopup.classList.toggle('active');
            document.body.classList.toggle('active');
            const postId = postCommentButton.dataset.postId;
            const workItem = document.querySelector(`.work-item-help-div[data-post-id="${postId}"]`);
            //获取页面中的文章元素
            const workThumbnail = document.querySelector('.work-thumbnail img');
            const currentPostCommentCount = workItem.querySelector('.currentPostCommentCount');
            const workTitle = workItem.querySelector('.work-title');
            const workDate = document.querySelector('.work-date');
            //获取评论框中的文章元素
            const itemImg = document.querySelector('.item-img');
            const mainTitle = document.querySelector('.main-title');
            const count = document.querySelector('.currentCount');
            const itemData = document.querySelector('.item-data');
            //统一
            console.log(currentPostCommentCount.textContent);
            count.textContent = currentPostCommentCount.textContent;
            mainTitle.textContent = workTitle.textContent;
            itemImg.src = workThumbnail.src;
            itemData.textContent = workDate.textContent;
            showAllCommentsEvent(postId);

        }

    })


//关闭评论弹窗
function hideCommentWindow(commentPopup) {
    commentPopup.classList.remove('active');
    document.body.classList.remove('active');
}
closeButton.addEventListener('click', () => hideCommentWindow(commentPopup));
// 绑定点击空白处隐藏评论框的事件
function bindOutsideClickEvent(commentPopup) {
    document.addEventListener('click', function (event) {
        if (!commentPopup.contains(event.target)) {
            hideCommentWindow(commentPopup);
        }
    });
}
bindOutsideClickEvent(commentPopup)
//点击按钮显示对应文章的第一页评论
const commentItemWrap = document.querySelector('.comment-item-wrap');
// postCommentButtons.forEach(postCommentButton => {
//     postCommentButton.addEventListener('click', () => {
//         const currentPostId = postCommentButton.querySelector('.postId').textContent;
//         console.log(currentPostId);
//         showAllCommentsEvent(currentPostId)
//     })
// })
let currentPageNum = 1;
function showAllCommentsEvent(postId) {
    commentItemWrap.innerHTML = '';
    currentPageNum = 1;// 每次显示评论区时重置为第一页
    $.ajax({
        url: `/post/selectComment/${postId}/1`,
        type: 'GET',
        data: {
            pageNum: 1,
            postId: postId
        },
        success: function (result) {
            console.log(result);
            result.data.comments.forEach(function (comment) {
                commentItemWrap.innerHTML += createCommentHtml(comment);
            })
            //重新绑定删除事件
            bindDeleteCommentEvents();
            if (result.data.hasMore) {
                shouldShowMoreButton = true;
                addMoreCommentButton(commentItemWrap, currentPageNum, postId);
            } else {
                shouldShowMoreButton = false;
            }

        },
        error: function (error) {
            console.error('全部评论获取失败', error);
        }
    });

}
// 创建评论 HTML 模板函数
function createCommentHtml(comment) {
    return `
    <div class="comment-item" data-comment-id="${comment.commentId}" >
    
                            <div class="item-header">
                                <img src="${comment.avatar}" alt="用户头像">
                                <span class="comment-id">${comment.userName}</span>
                            </div>
                            <div class="item-body">
                                <span>${comment.content}</span>

                            </div>
                            <div class="item-footer">
                                <div class="comment-time">
                                    <span>${comment.createdTime}</span>
                                </div>
                                <div class="comment-actions">
<!--                                    <div>-->
<!--                                        <i class="iconfont icon-comment"></i>-->
<!--                                        <button>回复</button>-->
<!--                                    </div>-->
                                    <div>
                                        <i class="iconfont icon-dianzan"></i>
                                        <span>${comment.likeCount}</span>
                                    </div>
                                    <div>
                                        <i class="iconfont icon-shanchu"></i>
                                        <button class="comment-delete-button">删除</button>
                                    </div>

                                </div>
                            </div>
                        </div>
    `;
}
//点击查看文章详情页
document.addEventListener('DOMContentLoaded', function () {
        workItemLists.addEventListener('click', (e) => {
            const workTitle = e.target.closest('.work-title');
            if(workTitle){
                console.log("一点击");
                const newsUserId = workTitle.dataset.userId;
                const newsPostId=workTitle.dataset.postId;
                if (newsPostId && newsUserId) {
                    console.log(newsPostId);
                    console.log(newsUserId);
                    goToEssayPage(newsPostId, newsUserId);
                } else {
                    console.error('未找到对应的 postId 或 userId 元素');
                }
            }

        });
    });

//删除评论
const confirmPopup = document.querySelector('.confirm-popup');
const deleteCloseButton = document.querySelector('.delete-close-button');
const cancelButton = document.querySelector('.cancel-button');
const confirmText = document.querySelector('.confirm-text');
const confirmButton = document.querySelector('.confirm-button');
function bindDeleteCommentEvents() {
    const commentItemWrap = document.querySelector('.comment-item-wrap');
    // 绑定到父容器，监听子元素的点击事件
    commentItemWrap.addEventListener('click', function (event) {
        // 检查点击事件是否发生在 .delete-myComment 元素上
        const commentDeleteButton = event.target.closest('.comment-delete-button');
        // console.log(commentDeleteButton)
        if (commentDeleteButton) {
            console.log("以点击删除按钮");
            const commentItem = commentDeleteButton.closest('.comment-item');
            const currentCommentId = commentItem.dataset.commentId;
            console.log('要删除的评论ID:', currentCommentId);

            // 处理删除逻辑
            confirmPopup.classList.add('active');
            confirmText.textContent = "确定删除此评论吗?";

            // 在确认按钮的点击事件中使用commentId
            confirmButton.onclick = function () {
                deleteComment(currentCommentId); // 调用删除函数
                closePopup(); // 关闭弹窗
            };
            deleteCloseButton.addEventListener('click', closePopup);
            cancelButton.addEventListener('click', closePopup);
        }
    });
}
//删除评论函数
function deleteComment(commentId) {
    console.log('commentId的数据类型:', typeof commentId);//"string"
    $.ajax({
        url: `/NewsController/deleteComment/${commentId}`,
        type: "POST",
        data: {
            commentId: commentId
        },
        success: function (result) {
            console.log(result);
            //移除评论元素
            const commentElement = document.querySelector(`.comment-item[data-comment-id="${commentId}"]`);
            if (commentElement) {
                commentElement.remove();
                showAlert("删除成功", 'green');
            } else {
                // console.log('未找到要删除的评论元素');
                showAlert("删除失败", 'red');
            }
        },
        error: function (error) {
            console.log(error);

        }
    })
}
// 评论的无限滚动
let isLoading = false; // 防止重复加载的标志
function loadMoreComments(postId, currentPageNUm) {
    if (isLoading) return;
    isLoading = true;
    currentPageNUm += 1;
    $.ajax({
        url: `/post/selectComment/${postId}/${currentPageNUm}`,
        type: 'GET',
        data: {
            pageNum: currentPageNUm,
            postId: postId
        },
        success: function (result) {
            console.log(result);
            console.log(result.data.hasMore);
            // 移除按钮
            const moreCommentButton = document.getElementById('more-comment-btn');
            if (moreCommentButton) {
                console.log("获取到按钮");
                moreCommentButton.remove();
            }

            result.data.comments.forEach(function (comment) {
                commentItemWrap.innerHTML += createCommentHtml(comment);
            });
            if (result.data.hasMore) {
                shouldShowMoreButton = true;
                addMoreCommentButton(commentItemWrap, currentPageNUm, postId);
            } else {
                shouldShowMoreButton = false;
                console.log("暂无更多评论");
                const noMoreComments = document.createElement('div');
                noMoreComments.classList.add('noMoreComments');
                noMoreComments.textContent = "暂无更多评论";
                commentItemWrap.appendChild(noMoreComments);
            }
            // 重新绑定删除事件
            bindDeleteCommentEvents();
            isLoading = false;
        },
        error: function (error) {
            console.error('加载更多评论失败', error);
            isLoading = false;
        }
    });
}
//添加"显示更多评论"按钮
function addMoreCommentButton(commentListElement, currentPageNum, postId) {
    console.log(shouldShowMoreButton);
    if (!shouldShowMoreButton) return;
    console.log("要显示按钮了");
    const moreCommentButton = document.createElement('button');
    moreCommentButton.type = 'button';
    moreCommentButton.id = 'more-comment-btn';
    moreCommentButton.textContent = "显示更多评论";
    moreCommentButton.classList.add('more-comment-button');
    moreCommentButton.addEventListener('click', function (event) {
        event.preventDefault(); // 阻止默认事件
        console.log("元素默认行为已取消");
        loadMoreComments(postId, currentPageNum);
    });
    commentListElement.appendChild(moreCommentButton);
}
//关闭弹窗
function closePopup() {
    console.log("已关闭")
    confirmPopup.classList.add('closing');
    setTimeout(() => {
        confirmPopup.classList.remove('active');
        confirmPopup.classList.remove('closing');
    }, 300); // 动画时长为 0.5s，所以这里设置 500ms
}
//删除作品
document.addEventListener('DOMContentLoaded', function () {
        workItemLists.addEventListener('click', (e) => {
            const postDeleteButton=e.target.closest('.post-delete-button');
            if(postDeleteButton){
                confirmPopup.classList.add('active');
                confirmText.textContent = "确定删除此作品吗?";
                const currentPostId = postDeleteButton.dataset.postId;
                console.log(currentPostId);
                // 在确认按钮的点击事件中使用commentId
                confirmButton.onclick = function () {
                    deletePost(currentPostId); // 调用删除函数
                    closePopup(); // 关闭弹窗
                };
                deleteCloseButton.addEventListener('click', closePopup);
                cancelButton.addEventListener('click', closePopup);
            }

        })
    });

    function deletePost(postId) {
        $.ajax({
            url: `/NewsController/deletePost/${postId}`,
            type: "POST",
            data: {
                postId: postId
            },
            success: function (result) {
                console.log(result);
                if (result == "删除成功") {
                    const parentElement = document.querySelector('.work-item-lists');
                    console.log(parentElement);
                    if (parentElement) {
                        const workItems = parentElement.querySelectorAll(`.work-item-help-div[data-post-id="${postId}"]`);
                        console.log(workItems);
                        if (workItems.length > 0) {
                            const workItem = workItems[0];
                            workItem.remove();
                            showAlert("删除成功", 'green');

                        }
                    }

                } else {
                    showAlert("删除失败", 'red');

                }

            },
            error: function (error) {
                console.log(error);
            }
        })
    }

//修改文章
        workItemLists.addEventListener('click', (event) => {
            const modifyButton=event.target.closest('.modify');
            if(modifyButton){
                event.preventDefault();
                console.log("已点击");
                const modifyPostId = modifyButton.dataset.postId;
                $.ajax({
                    url: `/getPostById/${modifyPostId}`,
                    type: "GET",
                    data: {
                        postId: modifyPostId,
                    },
                    success: function (result) {
                        console.log(result);
                        localStorage.setItem('editPostId', modifyPostId);
                        location.href = "/NewsController/toPost";
                    },
                    error: function (error) {
                        console.log(error);
                    }
                })
            }

        })
